package cn.zmlio.mybatis;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

import javax.sql.DataSource;

/**
 * @author : zml
 * @version : 1.0
 * Description :
 * Created in: 2018/5/17 下午2:01
 * Modified By:
 * @since : 1.0
 */
public class SpringConfiguration {
    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()
                .setType(EmbeddedDatabaseType.H2)
                .addScript("classpath:/schema/schema.sql")
                .build();
    }


    @Bean
    public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
        String resource = "config/mybatis-config.xml";
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setConfigLocation(new ClassPathResource(resource));
        return sessionFactory;
    }


}
